package com.cloud.sysmana.entity.dao.impl;

import java.util.List;

import com.cloud.portmana.ModuleConstant;
import com.cloud.sysmana.dto.SysStore;
import com.cloud.sysmana.entity.SysUser;
import com.cloud.sysmana.entity.dao.ISysUserDao;
import com.cloud.util.base.BaseHibernateDao;
import com.cloud.util.other.StringUtil;
import com.cloud.util.page.PageInfo;
import com.sun.org.apache.regexp.internal.recompile;

/**
 * 系统用户Dao
 * @author gaof
 * @date 2016年4月17日
 */
public class SysUserDao extends BaseHibernateDao<SysUser, Integer> implements ISysUserDao {

	@Override
	public SysUser getUserByNo(String number) {
		return this.getEntity("From SysUser where userAccount = ?", number);
	}
	
	/**
	 * 获取系统用户分页数据SQL
	 * @param pageInfo
	 * @return
	 */
	public PageInfo listUserPage(PageInfo pageInfo, SysStore dataStore){
		
		String hsql = "FROM SysUser";
		String countSql = "SELECT COUNT(*) FROM SysUser";
		String whereSql = " where 1=1";
		
		if (dataStore != null) {
			
			if (!StringUtil.isEmpty(dataStore.getUserName())) {
				whereSql += " and userName like '%" +StringUtil.addQuote(dataStore.getUserName())+ "%'";
			}
		}
		
		pageInfo.setTotalCountHbSQL(countSql + whereSql);
		pageInfo.setListHbSql(hsql + whereSql);
		
		return pageInfo;
	}
	
	/**
	 * 通过Id获取系统用户
	 * @param mid
	 * @return
	 */
	public SysUser getSysUser(int mid){
		return this.getEntity("from SysUser where userId=" + mid);
	}
	
	/**
	 * 删除系统用户
	 * @param sysUser
	 */
	public void delSysUser(SysUser sysUser){
		this.deleteEntity(sysUser);
	}
	
	/**
	 * 通过Id获取系统用户
	 * @param id
	 * @return
	 */
	public SysUser getSysUserById(Integer id){
		return this.findById(id);
	}
	
	/**
	 * 更新用户
	 * @param sysUser2
	 */
	public void updateSysUser(SysUser sysUser){
		this.updateEntity(sysUser);
	}

	/**
	 * 添加系统用户
	 * @param sysUser
	 * @return
	 */
	@Override
	public SysUser addSysUser(SysUser sysUser) {
		return this.saveEntity(sysUser);
	}

	/**
	 * 根据用户所在单位这条线的集合  结合职位 确定下一个审核人
	 * @param strIn
	 * @param flowRole
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<SysUser> getNextCheckPerUIdByStrInAndflowRole(String strIn,
			String flowRole) {
		// TODO Auto-generated method stub
		String sql = "select * from sys_user where role = '"+flowRole+"' and depar in ("+strIn+")";
		System.out.println("=========="+sql);
		/*return ((SysUser)this.getSQLQuery(sql, SysUser.class).get(0)).getUserId();*/
		return this.getSQLQuery(sql, SysUser.class);
	}

	
	@SuppressWarnings("rawtypes")
	@Override
	public List getUserJobNameById(Integer id) {
		// TODO Auto-generated method stub
		String sqlString="select j.JOB_NAME from police_user_job j where j.ID="+id;
		return this.getSQLQuery(sqlString);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysUser> getSysUserByDeparId(Integer deparId) {
		// TODO Auto-generated method stub
		String sqlString="select * from sys_user su where su.depar_id="+deparId+" and su.role='"+ModuleConstant.Charge+"'";
		return this.getSQLQuery(sqlString, SysUser.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysUser> getSysUserCharge1ByDeparId(Integer deparId) {
		// TODO Auto-generated method stub
		String sqlString="select * from sys_user su where su.depar_id="+deparId+" and su.role='"+ModuleConstant.Charge1+"'";
		return this.getSQLQuery(sqlString, SysUser.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysUser> getCharge(String deparId, String userJob) {
		// TODO Auto-generated method stub
		String sqlString="select * from sys_user su where su.depar_id="+deparId+" and su.role='"+userJob+"'";
		return this.getSQLQuery(sqlString, SysUser.class);
	}

	

}
